﻿Imports SharpSvn

Module modSVN
    Public Function SVN_HasUpdate(ByVal localPath As String, ByVal addressRepository As String) As Boolean

        Dim remoteTarget As SvnUriTarget
        Dim remoteInfo As SvnInfoEventArgs = Nothing
        Dim remoteClient As New SvnClient
        Dim remoteVersion As String

        Dim localTarget As SvnPathTarget
        Dim localInfo As SvnInfoEventArgs = Nothing
        Dim localClient As New SvnClient
        Dim localVersion As String

        Dim _compare As versionResult

        localTarget = SvnPathTarget.FromString(localPath)
        If Not (localClient.GetInfo(localTarget, localInfo)) Then Return Nothing
        localVersion = localInfo.Revision.ToString

        remoteTarget = SvnUriTarget.FromString(addressRepository)
        If Not (remoteClient.GetInfo(remoteTarget, remoteInfo)) Then Return Nothing
        remoteVersion = remoteInfo.Revision.ToString

        _compare = Version_Compare(localVersion, remoteVersion)

        If _compare = versionResult.IsNewer Then
            Return True
        ElseIf _compare = versionResult.IsOlder Then
            Return False
        Else
            Return Nothing
        End If
    End Function

    Public Function SVN_GetRevision(ByVal targetPath As String, Optional targetInfo As SvnInfoEventArgs = Nothing) _
        As String

        Dim ssClient As New SvnClient
        Dim ssInfo As SvnInfoEventArgs = targetInfo
        Dim ssTarget As SvnTarget

        If URI_IsValid(targetPath) Then
            ssTarget = SvnUriTarget.FromString(targetPath)
        Else
            ssTarget = SvnPathTarget.FromString(targetPath)
        End If

        Application.DoEvents()
        If Not (ssClient.GetInfo(ssTarget, ssInfo)) Then
            Throw New ApplicationException("Unable to execute GetInfo method on SvnClient.")
        End If

        Return ssInfo.Revision.ToString
    End Function

    Public Function SVN_CheckOut(ByVal url As Uri, ByVal dir As String, Optional progressReturn As Integer = Nothing) _
        As Boolean

        Dim ssClient As New SvnClient
        Dim ssArgs As New SvnCheckOutArgs
        Dim ssReturn As Boolean
        Dim ssResult As SvnUpdateResult = Nothing

        ssArgs.Depth = SvnDepth.Infinity

        Application.DoEvents()

        Try
            ssReturn = ssClient.CheckOut(url, dir, ssResult)
        Catch ex As Exception
            Return False
        End Try

        Return ssReturn
    End Function

    Public Function SVN_Update(ByVal dir As String, Optional ByVal svnArgs As SvnUpdateArgs = Nothing) As Boolean

        Dim ssClient As New SvnClient
        Dim ssArgs As New SvnUpdateArgs
        Dim ssReturn As Boolean

        If Not (svnArgs Is Nothing) Then
            ssArgs = svnArgs
        Else
            ssArgs.Depth = SvnDepth.Infinity
        End If

        Application.DoEvents()
        ssReturn = ssClient.Update(dir, ssArgs)

        Return ssReturn
    End Function
End Module